package com.leyou.item.web;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.entity.CategoryBrand;
import com.leyou.item.service.BrandService;
import com.leyou.item.service.CategoryBrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    @Autowired
    private CategoryBrandService categoryBrandService;

    /**
     * 分页查询
     *
     * @param page
     * @param rows
     * @return
     */
    @GetMapping("/page")
    public ResponseEntity<PageDTO<BrandDTO>> pageQuery(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "5") Integer rows,
            @RequestParam(value = "key",required = false) String key) {
        return ResponseEntity.ok(this.brandService.pageQuery(page, rows,key));
    }


    /**
     * 根据分类id查询对应的品牌集合
     *
     * @param cid
     * @return
     */
    @GetMapping("/of/category")
    public ResponseEntity<List<BrandDTO>> queryBrandByCategory(
            @RequestParam("id") Long cid) {

        return ResponseEntity.ok(this.categoryBrandService.queryBrandByCategory(cid));
    }

    /**
     * 根据品牌id查询对应的品牌对象
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResponseEntity<BrandDTO> queryBrandById(@PathVariable("id") Long id) {
        return ResponseEntity.ok(this.brandService.queryBrandById(id));
    }

    /**
     * 品牌新增
     * @param brandDTO
     * @return
     */
    @PostMapping
    public ResponseEntity<Void> addBrand(
           BrandDTO brandDTO){

        this.brandService.addBrand(brandDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }


    /**
     * 品牌修改
     * @param brandDTO
     * @return
     */
    @PutMapping
    public ResponseEntity<Void> updateBrand(
            BrandDTO brandDTO){

        this.brandService.updateBrand(brandDTO);
        return ResponseEntity.ok().build();
    }


    /**
     * 根据品牌id删除品牌信息
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteById(
            @PathVariable("id")Long id){

        this.brandService.deleteById(id);

        return ResponseEntity.ok().build();
    }

    /**
     * 根据品牌id的集合查询对应的品牌的集合
     * @param ids
     * @return
     */
    @GetMapping("/list")
    public ResponseEntity<List<BrandDTO>> listBrandByIds(@RequestParam("ids")List<Long> ids){

        return ResponseEntity.ok(this.brandService.listBrandByIds(ids));
    }
}
